home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / expandbackquotes.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  1KB  |  78 lines

  1. #include "kiss.h"
  2.  
  3. static void addoutput (Stringstack *what, char *cmd)
  4. {
  5.     FILE
  6.     *f;
  7.     register char
  8.     *info = 0,
  9.     *cp;
  10.     char
  11.     buf [FILENAMELEN];
  12.  
  13.     if (! (f = popen (cmd, "r")) )
  14.     {
  15.     warning ("cannot start \"%s\" for backquote expansion", cmd);
  16.     return;
  17.     }
  18.     while (fgets (buf, FILENAMELEN - 1, f))
  19.     {
  20.     if (! info)
  21.         info = xstrdup (buf);
  22.     else
  23.     {
  24.         info = xrealloc (info, strlen (info) + 4 + strlen (buf));
  25.         strcat (info, " ");
  26.         strcat (info, buf);
  27.     }
  28.     }
  29.     if ( (laststatus = pclose (f)) && ! flags.supressstat)
  30.     printf ("[%s: exited with %d]\n", cmd, laststatus);
  31.  
  32.     if (! info || ! *info)
  33.     return;
  34.  
  35.     if (! (cp = strtok (info, " \n\t")) )
  36.     addstringtostack (what, info);
  37.     else
  38.     {
  39.     addstringtostack (what, cp);
  40.     while ( (cp = strtok (NULL, " \n\t")) )
  41.         addstringtostack (what, cp);
  42.     }
  43.  
  44.     free (info);
  45. }
  46.  
  47. Stringstack expandbackquotes (Stringstack s)
  48. {
  49.     Stringstack
  50.     ret = { NULL, 0 };
  51.     register int
  52.     len,
  53.     i;
  54.     char
  55.     cmd [FILENAMELEN];
  56.  
  57.     for (i = 0; i < s.nstr; i++)
  58.     {
  59.     if (*s.str [i] != '`')
  60.     {
  61.         addstringtostack (&ret, s.str [i]);
  62.         continue;
  63.     }
  64.     strcpy (cmd, s.str [i] + 1);
  65.     len = strlen (cmd);
  66.     if (len < 2 || cmd [len - 1] != '`')
  67.     {
  68.         warning ("bad backquote syntax in \"%s\"", s.str [i]);
  69.         continue;
  70.     }
  71.     cmd [len - 1] = '\0';
  72.     addoutput (&ret, cmd);
  73.     }
  74.  
  75.     return (ret);
  76. }
  77.     
  78.